﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace Quan_Ly_Dai_Ly
{
    public partial class ThemMatHang : Office2007Form
    {
        public ThemMatHang()
        {
            InitializeComponent();
        }

        private void ThemMatHang_Activated(object sender, EventArgs e)
        {
            try
            {
                MainForm.Connect.Open();
                DataTable dt_DonVi = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter("select * from DONVITINH order by CAST(TenDonViTinh as nvarchar(100)) asc", MainForm.Connect);
                da.Fill(dt_DonVi);
                cb_DonVi.DataSource = dt_DonVi;
                cb_DonVi.DisplayMember = "TenDonViTinh";
                cb_DonVi.ValueMember = "MaDonViTinh";
                MainForm.Connect.Close();
            }
            catch (SqlException sqlEx)
            {
                MainForm.Connect.Close();
                MessageBoxEx.Show("Lỗi kết nối cơ sở dữ liệu!\n" + sqlEx.Message, "Thông Báo");
                Close();
            }
        }
       
        private void bt_DongY_Click(object sender, EventArgs e)
        {
            int result = MainForm.ExecProcedure("sp_ThemMatHang",
                            new string[]{"@TenMatHang", "@MaDonViTinh", "@DonGia"},
                            new object[] { tb_Ten.Text, cb_DonVi.SelectedValue, tb_DonGia.Text});
            if (result == 1)
                MessageBoxEx.Show("Thêm mặt hàng không thành công.\nMặt hàng này đã có trong cơ sở dữ liệu.", "Thông Báo");
            else
                if (result == 0)
                    MessageBoxEx.Show("Thêm mặt hàng mới thành công.", "Thông Báo");
            tb_Ten.ResetText();
            tb_DonGia.ResetText();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            Form f = new ThemDonViTinh();
            f.MdiParent = this.MdiParent;
            f.Show();
        }

        private void tb_Ten_TextChanged(object sender, EventArgs e)
        {
            if (tb_Ten.Text.Length > 0 && cb_DonVi.Text.Length > 0 && tb_DonGia.Text.Length > 0)
                bt_DongY.Enabled = true;
            else
                bt_DongY.Enabled = false;
        }

        private void cb_DonVi_TextChanged(object sender, EventArgs e)
        {
            if (tb_Ten.Text.Length > 0 && cb_DonVi.Text.Length > 0 && tb_DonGia.Text.Length > 0)
                bt_DongY.Enabled = true;
            else
                bt_DongY.Enabled = false;
        }

        private void tb_DonGia_TextChanged(object sender, EventArgs e)
        {
            if (tb_Ten.Text.Length > 0 && cb_DonVi.Text.Length > 0 && tb_DonGia.Text.Length > 0)
                bt_DongY.Enabled = true;
            else
                bt_DongY.Enabled = false;
        }

        private void tb_DonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

    }
}
